//
//  AlipayTopView.m
//  Alipay
//
//  Created by 张强 on 16/8/31.
//  Copyright © 2016年 张强. All rights reserved.
//

#import "AXFIconView.h"
#import "AXFDataModel.h"

@implementation AXFIconView

- (instancetype)initWithFrame:(CGRect)frame model:(NSArray<AXFDataModel *> *)model
{
    self = [super initWithFrame:frame];
    if (self) {
        [self alipayTopViewUIWith:model];
    }
    return self;
}

/*!
 * 创建四个按钮
 */
- (void)alipayTopViewUIWith:(NSArray<AXFDataModel *> *)model
{
    [self addButtonWithImageName:model[0].imgURL title:model[0].name tag:AXFIconViewButtonTypeCard];
    [self addButtonWithImageName:model[1].imgURL title:model[1].name tag:AXFIconViewButtonTypePay];
    [self addButtonWithImageName:model[2].imgURL title:model[2].name tag:AXFIconViewButtonTypeScan];
    [self addButtonWithImageName:model[3].imgURL title:model[3].name tag:AXFIconViewButtonTypeXiu];
}
/*!
 * 图文魂牌按钮
 * @param imageName 图片名字
 * @param title 文字
 * @param tag 按钮tag属性
 */
- (void)addButtonWithImageName:(NSString *)imageName title:(NSString *)title tag:(AXFIconViewButtonType)tag
{
    UIButton *button = [[UIButton alloc] init];
    
    //[button.imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"redCycle"]];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
//    //设置按钮图文混排
    [button setAttributedTitle:[NSAttributedString mh_imageTextWithImage:[UIImage imageNamed:imageName] imageW:60 imageH:60 title:title fontSize:14 titleColor:[UIColor blackColor] spacing:7] forState:UIControlStateNormal];
//
//    //多行
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
//
//    
//    //自适应
    [button sizeToFit];
//    button.tag = tag;
    
    [button addTarget:self action:@selector(topViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button];
}
/*!
 * 设置每个button的位置
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    //计算每个图标之间的间隔
    CGFloat padding = ([UIScreen mainScreen].bounds.size.width - self.subviews[0].bounds.size.width * 4) / 5;
    for (int i = 0; i < self.subviews.count - 1; i++)
    {
        UIButton *firstButton = self.subviews[i];
        UIButton *nextButton = self.subviews[i + 1];
        
        //如果是第一个按钮
        if(i == 0)
        {
            [firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self).offset(padding);
                make.centerY.equalTo(self);
            }];
        }
        [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(firstButton.mas_right).offset(padding);
            make.centerY.equalTo(self);
        }];
    }
}

/*!
 * 按钮TouchUpInside方法监听
 */
- (void)topViewButtonClick:(UIButton *)sender
{
    [self.delegate AXFIconViewButtonClick:sender];
    NSLog(@"sspspspsp");
}

@end
